<?php

namespace app\admin\controller;

use app\BaseController;
use app\model\ConsultOfflineModel;
use app\model\ConsultOnlineModel;
use app\model\ProxyEvaluationResultModel;
use app\model\SelfEvaluationResultModel;
use app\model\TaskUserResultModel;
use app\model\WarningReportRecordModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 咨询消息
     * @return bool|string
     */
    public function ConsultMessage(): bool|string
    {
        try {
            $ConsultOnlineModel = new ConsultOnlineModel();
            $ConsultOfflineModel = new ConsultOfflineModel();
            if ($this->userData['role_id'] == 0){
                $where[] = ['unit_id','=',$this->userData['unit_id']];
            }else{
                $where[] = ['consult_id','=',$this->userData['id']];
            }
            $data = $ConsultOnlineModel->field('id,question,time,1 as type,uid,0 as time_id')
                ->where($where)
                ->with([
                    'user' => function($query){
                        $query->with(['departmentName']);
                    }
                ])
                ->order('id desc')->limit(0,4)->select()->toArray();
            $data2 = $ConsultOfflineModel->field('id,question,time,2 as type,uid,time_id')
                ->where($where)
                ->with([
                    'user' => function($query){
                        $query->with(['departmentName']);
                    },
                    'ConsultTime'
                ])
                ->order('id desc')->limit(0,4)->select()->toArray();
            foreach ($data2 as &$value){
                $value['date'] = date('Y-m-d',$value['reservation_start_time']);
                $value['period'] = date('H:i',$value['reservation_start_time']).'-'.date('H:i',$value['reservation_end_time']);
            }
            $data3 = array_merge($data,$data2);
            $field = array_column($data3,'time');
            array_multisort($field, SORT_DESC, $data3);
            $data4 = array_slice($data3, 0, 4);
            return success($data4);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 预警消息
     * @return bool|string
     */
    public function WarningReport(): bool|string
    {
        try {
            $WarningReportRecordModel = new WarningReportRecordModel();
            $TaskUserResultModel = new TaskUserResultModel();
            $SelfEvaluationResultModel = new SelfEvaluationResultModel();
            $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
            $data = $WarningReportRecordModel->field('id,source,result_id,time')
                ->where('unit_id','=',$this->userData['unit_id'])
                ->order('id desc')->limit(0,4)->select()->toArray();
            foreach ($data as &$item) {
                switch ($item['source']) {
                    case 1:
                        $source = '任务测评';
                        $info = $TaskUserResultModel->with([
                            'user' => function ($query) {
                                $query->with(['departmentName']);
                            },
                            'RatingScaleFactorWarningModel',
                            'task',
                            'RatingScale' => function ($query) {
                                $query->with(['ratingScaleTypeName']);
                            }
                        ])->find($item['result_id']);
                        if (!$info) throw new \Exception('有报告被删除');
                        break;
                    case 2:
                        $source = '自我测评';
                        $info = $SelfEvaluationResultModel->with([
                            'user' => function ($query) {
                                $query->with(['departmentName']);
                            },
                            'RatingScaleFactorWarningModel',
                            'RatingScale' => function ($query) {
                                $query->with(['ratingScaleTypeName']);
                            }
                        ])->find($item['result_id']);
                        if (!$info) throw new \Exception('有报告被删除');
                        break;
                    case 3:
                        $source = '他人代测';
                        $info = $ProxyEvaluationResultModel->with([
                            'user' => function ($query) {
                                $query->with(['departmentName']);
                            },
                            'RatingScaleFactorWarningModel',
                            'RatingScale' => function ($query) {
                                $query->with(['ratingScaleTypeName']);
                            }
                        ])->find($item['result_id']);
                        if (!$info) throw new \Exception('有报告被删除');
                        break;
                    default:
                        throw new \Exception('系统错误！');
                }
                $item = [
                    'id' => $item['id'],
                    'rating_scale_name' => $info['rating_scale_name'],
                    'source' => $source,
                    'task_name' => $info['task_name'],
                    'user_name' => $info['user_name'],
                    'department_name' => $info['department_name'],
                    'time' => $item['time'],
                    'is_warning' => $info['is_warning'],
                    'warning_name' => $info['warning_name']??''
                ];
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 屏保验证密码
     * @return bool|string
     */
    public function VerifyPassword(): bool|string
    {
        try {
            $password = $this->request->param('password');
            if (encrypt_password($password) != $this->userData['password']) throw new \Exception('密码错误！');
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

}